/** 
 * 自动重启hack脚本
 * 为hack等级自动重启hack能换掉以前的hack目标
 * 端口工具有更新时, 能使用更多的野生服务器, 那时也需要重启hack
 * 内存大小没有没有10T, 也是切换脚本的关键
 * 端口数的变化是有效的, 但是, 会有些报错, 没看清报错是什么
 */

import {get_target_server} from "/hack_prepare/get-target-server.js"
//import {all_execute_server} from "/hack_prepare/all-execute-server.js"

function get_port_tools_number(ns)
{
  let result_number = 0;

  if(ns.fileExists("BruteSSH.exe","home"))
  {
    ++result_number;
  }
  if(ns.fileExists("FTPCrack.exe","home"))
  {
    ++result_number;
  }
  if(ns.fileExists("relaySMTP.exe","home"))
  {
    ++result_number;
  }
  //HTTPWorm.exe
  if(ns.fileExists("HTTPWorm.exe","home"))
  {
    ++result_number;
  }
  //SQLInject.exe
  if(ns.fileExists("SQLInject.exe","home"))
  {
    ++result_number;
  }

  return result_number;
}

function run_in_home(ns)
{
  var home_max_ram = ns.getServerMaxRam('home');
  var scripts0 = ns.getRunningScript("/hack_prepare/dispatch-in-host.js");
  if(2000<home_max_ram)
  {
    // 判断脚本没在执行:
    var scripts1 = ns.getRunningScript("/hack_prepare/dispatch-in-home.js");
    if(scripts1)
    {
      ns.print("大内存 杀死原本的home中的进程 重新运行");
      ns.kill("/hack_prepare/dispatch-in-home.js");
      ns.exec("/hack_prepare/dispatch-in-home.js","home");
    }
    else
    {
      if(scripts0)
      {
        ns.print("大内存 杀死原本的home中的进程");
        ns.kill("/hack_prepare/dispatch-in-host.js");
      }
      ns.print("大内存 执行dispatch-in-home.js");
      ns.exec("/hack_prepare/dispatch-in-home.js","home");
    }
  }
  else
  {
    if(!scripts0)
    {
      ns.print("小内存,执行 dispatch-in-host.js ");
      ns.exec("/hack_prepare/dispatch-in-host.js","home");
    }
  }
}

export async function main(ns) {
  // 1. 端口工具数:
  let port_tool_number = 0;//get_port_tools_number(ns);
  // 2. hack的目标
  let last_target_name = await get_target_server(ns);
  
  
  do{
    let next_port_number = get_port_tools_number(ns);
    let next_target_name = await get_target_server(ns);
    ns.print("=====================================");
    if(next_port_number!=port_tool_number)
    {
      port_tool_number = next_port_number;
      ns.print("端口有不一样, 执行脚本:野生服务器的更新");
      ns.kill("/hack_prepare/grow-no-loop.js","home",next_target_name);
      ns.kill("/hack_prepare/hack-no-loop.js","home",next_target_name);
      ns.kill("/hack_prepare/weaken-no-loop.js","home",next_target_name);
      await ns.exec("/hack_prepare/deploy-all-server.js","home");
      run_in_home(ns);
    }
    // --------
    if(next_target_name!=last_target_name)
    {
      last_target_name = next_target_name;
      ns.print("目标服务器有不一样, 执行脚本:野生服务器的更新");
      ns.kill("/hack_prepare/grow-no-loop.js","home",next_target_name);
      ns.kill("/hack_prepare/hack-no-loop.js","home",next_target_name);
      ns.kill("/hack_prepare/weaken-no-loop.js","home",next_target_name);
      await ns.exec("/hack_prepare/deploy-all-server.js","home");
      run_in_home(ns);
    }
    // 防范 如果到最后, 两种脚本都没执行:
    var scripts0 = ns.getRunningScript("/hack_prepare/dispatch-in-host.js");
    var scripts1 = ns.getRunningScript("/hack_prepare/dispatch-in-home.js");
    if(!scripts0 && !scripts1 )
    {
      run_in_home(ns);
    }
    ns.print("=====================================");
    ns.print("当前hack的目标:",last_target_name,"\t端口工具数:",next_port_number);
    await ns.sleep(10000);
  }while(true);
}